package com.software.sqlite.service.impl;

import com.software.sqlite.api.admin.v1.vo.AdminLoginUserVO;
import com.software.sqlite.common.BaseMapper;
import com.software.sqlite.entity.TbSysUser;
import com.software.sqlite.mapper.TbSysUserMapper;
import com.software.sqlite.service.ITbSysUserService;
import com.software.sqlite.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Slf4j
public class TbSysUserService extends BaseService<TbSysUser,String,Object> implements ITbSysUserService {

    @Resource
    private TbSysUserMapper mapper;
    @Override
    protected BaseMapper<TbSysUser> getBaseMapper() {
        return mapper;
    }
    @Value("${file.basedir}")
    private String basedir;

    @Override
    public TbSysUser getLogin(String userName, String password){
        return mapper.getLogin(userName,password);
    }


    @Override
    public boolean edit(TbSysUser model) {
        boolean result=false;
        try{
            if(model!=null){
                if(CommonUtils.isNotBlank(model.getOrgName()) && model.getOrgName().equals("全部门"))
                {
                    model.setOrgName("");
                    model.setOrgId(0L);
                }
                AdminLoginUserVO loginUser=this.getLoginUser(model.getToken());
                model.setUseStatus("normal");
                if(model.getUserId()==null){
                    TbSysUser sysUser=mapper.getUserByName(model.getUserName());
                    if(sysUser!=null){
                        return result;
                    }
                    model.setCreateTime(new Date());
                    model.setCreateAdminId(loginUser.getUserId());
                    add(model);
                }else{
                    model.setUpdateTime(new Date());
                    model.setUpdateAdminId(loginUser.getUserId());
                    model.setCreateTime(new Date());
                    model.setCreateAdminId(loginUser.getUserId());
                    update(model);
                }
                result=true;
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return result;
    }

    @Override
    public boolean updatePwd(Long userId, String password, String newPassword) {
        boolean result=false;
        try
        {
            TbSysUser sysUser=mapper.selectByPrimaryKey(userId);
            if(sysUser!=null && sysUser.getPassword().equals(password) && CommonUtils.isNotBlank(newPassword))
            {
                sysUser.setPassword(newPassword);
                mapper.updateByPrimaryKey(sysUser);
                result=true;
            }

        }catch (Exception e){

        }
        return result;
    }

    @Override
    public void importData(TbSysUser model) throws Exception {
        log.info("importData...");
    }

    @Override
    public void exportData(TbSysUser model) throws Exception {
        log.info("exportData...");
    }
}
